#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"
void initdraw()
{
	initgraph(ROW+20, COL+20);
	setbkcolor(GREEN);
	cleardevice();
	setlinecolor(RED);
	setfillcolor(RED);
	fillrectangle(0, 0, ROW + 20, 10);
	fillrectangle(0, 0, 10, COL + 20);
	fillrectangle(0, COL + 10, ROW + 20, COL + 20);
	fillrectangle(ROW + 10, 0, ROW + 20, COL + 20);
	
	//closegraph();
}


void initsnake()
{
	for (int i = 0; i < ROW * COL / 100; i++)
	{
		Body[i].BodyX = 0;
		Body[i].BodyY = 0;
	}
	Head.HeadX = ROW / 2;
	Head.HeadY = COL / 2;
	Head.len = 3;
	Body[0].BodyX = ROW / 2;
	Body[0].BodyY = COL / 2;
	Body[1].BodyX = ROW / 2-2 * SNAKER;
	Body[1].BodyY = COL / 2;
	Body[2].BodyX = ROW / 2 - 4 * SNAKER;
	Body[2].BodyY = COL / 2;
	

}
void drawsnake()
{
	setfillcolor(BLUE);
	for (int i = 0; i < Head.len; i++)
	{
		fillcircle(Body[0].BodyX, Body[0].BodyY, SNAKER);
		
	}
}
void randfood()
{
	int x = rand() % ROW;
	int y = rand() % COL;
	setfillcolor(BLACK);
	solidcircle(x, y, SNAKER);
}

void snakemove()
{
	while (1)
	{
		if (_kbhit())
		{
			aa:
			char key = _getch();
			switch (key)
			{
			case 'W':
			case'w':
			case UP:
					for (int i = 1; i < Head.len; i++)
					{
						Body[i] = Body[i - 1];
					}
					Body[0].BodyY--;
					Head.HeadY--;
					drawsnake();

					goto aa;
			case's':
			case'S':
			case DOWN:
				for (int i = 1; i < Head.len; i++)
				{
					Body[i] = Body[i - 1];
				}
				Body[0].BodyY++;
				Head.HeadY++;
				drawsnake();
				goto aa;
			case'a':
			case'A':
			case LEFT :
				for (int i = 1; i < Head.len; i++)
				{
					Body[i] = Body[i - 1];
				}
				Body[0].BodyX--;
				Head.HeadX--;
				drawsnake();

				goto aa;
			case'd':
			case'D':
			case RIGHT:
				for (int i = 1; i < Head.len; i++)
				{
					Body[i] = Body[i - 1];
				}
				Body[0].BodyX++;
				Head.HeadX++;
				drawsnake();

				goto aa;
			}

		}
	}
}